Popular Searches
Popular Course Categories
Popular Courses

Basic syntax, main() function, statements, and comments

Basic syntax, main() function, statements, and comments

Introduction to Dart

Basic Syntax, main() Function, Statements, and Comments in Dart

Dart is the programming language used with Flutter to build cross-platform applications. JustAcademy's Flutter curriculum introduces Dart programming fundamentals as part of the Flutter learning path, including variables, data types, operators, control statements, functions, object-oriented programming, collections, and asynchronous programming. :contentReference[oaicite:0]{index=0}

In this chapter, you will learn the basic structure of Dart programs, the main() function, statements, semicolons, comments, code blocks, and common syntax rules that form the foundation of Flutter development.

Course: JustAcademy Flutter Training

Demo: Register for Flutter Course Demo


1. What Is Dart Syntax?

Syntax refers to the rules used to write valid Dart programs. Just as English has grammar rules, Dart has rules for declaring variables, calling functions, writing statements, creating classes, and controlling program flow.

For example:

void main() {
  print("Hello, Dart!");
}

This small program contains several important Dart syntax elements:

  • void represents the return type of the function.
  • main() is the entry-point function.
  • { } defines the function body.
  • print() is used to display output.
  • ; terminates the statement.

2. Basic Structure of a Dart Program

A simple Dart program can be written as:

void main() {
  print("Welcome to Dart");
}

The general structure is:

returnType functionName() {
  // statements
}

Dart programs can contain variables, functions, classes, objects, control-flow statements, collections, imports, and other language features.

Example

void main() {
  String name = "Rahul";
  int age = 22;

  print(name);
  print(age);
}

3. The main() Function

The main() function is the starting point of a Dart application. When a Dart program is executed, program execution begins from main().

void main() {
  print("Program started");
}

Output:

Program started

Understanding main()

void main() {
  print("Hello");
}
  • void is the return type.
  • main is the function name.
  • () represents the function's parameter list.
  • { } contains the statements executed by the function.

main() With Statements

void main() {
  print("Dart Programming");
  print("Flutter Development");
  print("Learning Dart Syntax");
}

Output:

Dart Programming
Flutter Development
Learning Dart Syntax

4. main() With Variables

Variables can be declared inside the main() function and then used by the statements within the function.

void main() {
  String studentName = "Amit";
  int marks = 85;

  print(studentName);
  print(marks);
}

5. main() With Parameters

Dart also supports a form of main() that receives command-line arguments.

void main(List arguments) {
  print(arguments);
}

The arguments parameter can contain values supplied when the Dart application is launched from a command-line environment.

6. What Are Statements?

A statement is an instruction that tells the Dart program to perform an action. Most Dart statements end with a semicolon (;).

Example

void main() {
  int age = 25;
  print(age);
}

Here:

  • int age = 25; is a variable declaration statement.
  • print(age); is a function-call statement.

7. Semicolon in Dart

A semicolon generally marks the end of a Dart statement.

String name = "John";
int age = 30;
print(name);
print(age);

Forgetting a required semicolon can cause a syntax error.

Correct

int age = 20;
print(age);

Incorrect

int age = 20
print(age);

Therefore, understanding where statements end is an important part of learning Dart syntax.

8. Expression vs Statement

An expression produces a value, while a statement represents an instruction or complete action.

Expression

10 + 20

Statement

int total = 10 + 20;

The expression 10 + 20 produces the value 30, while the complete statement stores that value in the variable total.

9. Code Blocks

Curly braces { } are used to define blocks of Dart code. They are commonly used with functions, conditions, loops, and classes.

void main() {
  print("Inside main function");
}

The statements between { and } form the function body.

Code Block With if

void main() {
  int age = 20;

  if (age >= 18) {
    print("Adult");
  }
}

10. Comments in Dart

Comments are notes written inside source code for developers. They are ignored during normal program execution and are useful for explaining code, documenting logic, and temporarily disabling code.

Dart supports three commonly used comment styles:

  1. Single-line comments
  2. Multi-line comments
  3. Documentation comments

11. Single-Line Comments

A single-line comment begins with two forward slashes: //

// This is a comment
print("Hello Dart");

Everything after // on that line is treated as a comment.

Example

void main() {
  // Store the student's name
  String name = "Rahul";

  // Display the name
  print(name);
}

12. Multi-Line Comments

Multi-line comments begin with /* and end with */.

/*
  This is a multi-line comment.
  It can contain multiple lines.
*/

void main() {
  print("Hello Dart");
}

Multi-line comments are useful when a longer explanation is required.

13. Documentation Comments

Documentation comments are commonly used to describe functions, classes, variables, and APIs.

They can be written using ///:

/// Calculates the total price.
double calculateTotal(double price, double tax) {
  return price + tax;
}

Documentation comments can be useful when creating reusable Dart libraries and documenting APIs.

14. Comments Inside a Program

Comments can appear before statements:

void main() {
  // Create a variable
  int number = 10;

  print(number);
}

They can also appear after code:

int age = 25; // Student age

15. Variables and Basic Syntax

Variables are used to store values. Dart supports explicit type declarations and type inference.

String name = "Ankit";
int age = 21;
double percentage = 87.5;
bool isStudent = true;

Type inference can also be used:

var name = "Ankit";
var age = 21;
var percentage = 87.5;

16. String Syntax

Strings can be written using single or double quotation marks.

String name = "Rahul";
String city = 'Mumbai';

String interpolation uses the $ symbol:

String name = "Rahul";
print("Hello $name");

For expressions, use ${}:

int age = 20;

print("Next year I will be ${age + 1}");

17. Operators in Basic Syntax

Dart supports arithmetic, comparison, logical, and assignment operators.

int a = 10;
int b = 5;

print(a + b);
print(a - b);
print(a * b);
print(a / b);

Comparison Example

int age = 20;

print(age >= 18);
print(age == 20);

18. Conditional Statements

Conditional statements allow a program to execute code based on a condition. JustAcademy's Dart fundamentals curriculum includes control statements such as if, loops, and switch. :contentReference[oaicite:1]{index=1}

void main() {
  int marks = 75;

  if (marks >= 40) {
    print("Pass");
  } else {
    print("Fail");
  }
}

19. Loops

Loops are used to execute a block of code repeatedly.

for Loop

void main() {
  for (int i = 1; i <= 5; i++) {
    print(i);
  }
}

while Loop

void main() {
  int i = 1;

  while (i <= 5) {
    print(i);
    i++;
  }
}

20. Functions

Functions are reusable blocks of code designed to perform a specific task. Functions and parameters are part of the Dart fundamentals covered in the JustAcademy Flutter curriculum. :contentReference[oaicite:2]{index=2}

void greet() {
  print("Hello Dart");
}

void main() {
  greet();
}

Function With Parameter

void greet(String name) {
  print("Hello $name");
}

void main() {
  greet("Rahul");
}

Function With Return Value

int add(int a, int b) {
  return a + b;
}

void main() {
  int result = add(10, 20);
  print(result);
}

21. Arrow Function Syntax

A short function containing a single expression can use arrow syntax.

int square(int number) => number * number;

void main() {
  print(square(5));
}

Output:

25

22. Import Statements

Dart programs can import libraries when functionality from another library is required.

import 'dart:math';

void main() {
  print(sqrt(25));
}

In Flutter applications, imports are also used to access Flutter framework libraries and application files.

23. Basic Dart Program Example

void main() {
  // Student information
  String name = "Amit";
  int age = 22;
  double marks = 85.5;
  bool passed = true;

  // Display student information
  print("Name: $name");
  print("Age: $age");
  print("Marks: $marks");
  print("Passed: $passed");
}

24. Complete Example Using Syntax, Statements, and Comments

// Dart basic syntax example

void main() {
  // Declare variables
  String studentName = "Priya";
  int marks = 82;

  // Check result
  if (marks >= 40) {
    print("$studentName has passed.");
  } else {
    print("$studentName has failed.");
  }
}

Output:

Priya has passed.

25. Common Dart Syntax Rules

Rule Example Purpose
Entry point void main() { } Starts program execution
Statement terminator ; Ends most statements
Code block { } Groups statements
Single-line comment // comment Adds a one-line comment
Multi-line comment /* comment */ Adds a multi-line comment
Documentation comment /// comment Documents code
String "Hello" Stores text
Variable int age = 20; Stores a value
Function call print("Hello"); Executes a function

26. Common Beginner Mistakes

Mistake 1: Forgetting a Semicolon

// Incorrect
int age = 20

// Correct
int age = 20;

Mistake 2: Incorrect Function Structure

// Correct
void main() {
  print("Hello");
}

Mistake 3: Incorrect String Quotes

// Correct
String name = "Rahul";

Mistake 4: Confusing Comments With Code

// This line is ignored by Dart
print("This line is executed");

Mistake 5: Missing Braces

if (age >= 18) {
  print("Adult");
}

27. Best Practices for Writing Dart Syntax

  • Use meaningful variable and function names.
  • Keep functions focused on a specific task.
  • Use comments to explain important or non-obvious logic.
  • Use documentation comments for reusable APIs and public code.
  • Maintain consistent indentation and formatting.
  • Use appropriate data types instead of relying unnecessarily on dynamic.
  • Keep code readable and organized.
  • Use the Dart formatter available in Dart/Flutter development tools.

28. Dart Syntax and Flutter

Dart provides the programming language foundation, while Flutter provides the framework and widgets used to build application interfaces. JustAcademy's curriculum begins with Dart language fundamentals and then progresses into Flutter widgets, UI development, navigation, APIs, Firebase, state management, testing, deployment, and projects. :contentReference[oaicite:3]{index=3}

For example, a basic Flutter application still uses Dart syntax:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text("Hello Flutter"),
        ),
      ),
    );
  }
}

This example combines Dart concepts such as imports, the main() function, classes, constructors, methods, expressions, and statements with Flutter widgets.

29. Quick Revision

  • Dart is the programming language used by Flutter.
  • Program execution starts from the main() function.
  • Statements generally end with a semicolon.
  • Curly braces define code blocks.
  • // creates a single-line comment.
  • /* */ creates a multi-line comment.
  • /// is commonly used for documentation comments.
  • Variables store values that can be used by the program.
  • Functions group reusable logic.
  • Conditions and loops control program execution.
  • Imports provide access to libraries and other Dart files.

30. Practice Exercises

  1. Create a Dart program that prints your name, age, and city.
  2. Create a program that calculates the sum of two numbers.
  3. Write a program that checks whether a number is even or odd.
  4. Create a program using an if-else statement to check exam results.
  5. Write a for loop that prints numbers from 1 to 10.
  6. Create a function that accepts two numbers and returns their sum.
  7. Add single-line and multi-line comments to your program.
  8. Create a small student information program using variables and comments.

31. Key Takeaways

Basic Dart syntax is the foundation for learning Flutter development. Before building Flutter interfaces, it is important to understand how Dart programs are structured, how main() works, how statements are written, how code blocks are formed, and how comments are used.

These concepts provide the foundation for the broader Dart programming topics covered in the JustAcademy Flutter curriculum, including variables, data types, operators, control statements, functions, OOP, collections, and asynchronous programming. :contentReference[oaicite:4]{index=4}

32. Learn Flutter with JustAcademy

To continue learning Dart and Flutter through structured training, practical exercises, and project-based learning, visit:

JustAcademy Flutter Training

You can also register for a course demo here:

Register for Flutter Course Demo

whatsapp